unit w1025G_Iface;
(******************************************************************************
  w1025G_Iface.pas

  Delphi wrapper interface for the Hantek 1025G USB function/arbitrary wave
  form generator functions. Uses static loading.

  Version 1.0, created 5/20/2013.
******************************************************************************)

interface

{uses
  Winapi.Windows, Winapi.Messages, System.Classes, System.SysUtils, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, System.UITypes,
  VCLTee.TeEngine; }

const
  HT_MEM_PTS = 2048;     //Using this as current depth of pattern memory
  WFZERO = 2048;         //A cnt of 2048 sets Vout at 0V.  0 = 3.5V, 4095 = -3.5V.
type ptrWord = ^Word;

//DLL Function Declarations
 Function  DDSSearch(): Integer StdCall;
 Function  DDSSetFrequency(index: Integer; frequency: double; var wavePointNum: Integer; var TNum: Integer):Boolean StdCall;
 Function  DDSGetMeasure(index: Integer; bFreq: boolean; measure: pDouble):Boolean StdCall;
 Function  DDSSetSingleWave(index: Integer; bSingle: boolean):Boolean StdCall;
 Function  DDSResetCounter(index: Integer):Boolean StdCall;
 Function  DDSSetTrigger(index: Integer; bInter: Boolean; bEdge: Boolean):Boolean StdCall;
 Function  DDSGetDigitalIn(index: Integer; dIn: pSmallInt):Boolean StdCall;
 Function  DDSSetDigitalOut(index: Integer; dOut: SmallInt):Boolean StdCall;
 Function  DDSDownload(index: Integer; buf: ptrWord; number: Integer):Boolean StdCall;
 Function  DDSSetDIOMode(index: Integer; mode: Boolean):Boolean StdCall;
 Function  DDSCheck(index: Integer):Boolean StdCall;
 Function  DDSSetPowerOnOutput(index: Integer; bOpen: Boolean):Boolean StdCall;

(******************************************************************************
THT1025 Class Description
*****************************************************************************)
type THT1025 = class
  public
    mFound: Boolean;  //configure true if HT1025 is found on system.
    mHnd: Integer;     //Device number (1,2,3 etc. if more than one HT1025 in system)
    mWaveFrmPoints: LongInt;  //number of points in the waveform (calculated when loaded)
    mFreqPts: Integer;   //Only needed to call set freq function, which populates this value (not using value).
    mNumPer: LongInt;       //Number of periods in waveform (default to 1)
    mWaveForm: Array of Word;  //the current waveform to load to the 1025. Must set length prior to using.
    mWaveMin: Word;            //holds the minimum value of array (will be normalized to zero).
    mWaveMax: Word;           //holds the maximum value of array
    function mFind(): Integer;     //returns the number of HT1025's in the system.
    function mSetFreq(freq: double): Boolean;     //set the output freq, return true if successful.
            //Output freq, number of points in cycle, number of periods per output
    function mSingleCyc(val: Boolean):Boolean; //True = Single, False = continuous. Return true if operation successful
    function mLoadPat(buf: ptrWord): Boolean;  //loads HT waveform into device. Return true if successful.
    function mStatus(ID: Integer):Boolean;   //Call to verify that HT1025 is communicating. Return true if OK
    function mOutputOnStart(val: Boolean): Boolean;  //Set to true if waveform should be present on power up.
    function mSetTrigger(ext: Boolean; edge:Boolean): Boolean;   //Sets internal or external trigger, returns true if operation is successful.

end;

(******************************************************************************)

implementation

(******************************************************************************
HT1025 method implementations
*******************************************************************************)

(******************************************************************************
HTFind: Search for HT1025 units on the USB bus, and return the number of devices
found.
*******************************************************************************)
function THT1025.mFind;
begin
  mHnd := DDSSearch();
  if mHnd > 0 then
    begin
      dec(mHnd);   //Driver assigns num units found - 1 as address.
      mFound := True;
    end
    else
      mFound := False;
end;

(******************************************************************************
HTSetFreq: Set the desired output frequency of the waveform.
           freq: Desired frequency in Hz
           numPts: number of points in the waveform
           Per: Will default to 1 period per waveform.
           Result = true if operation is successful.
*******************************************************************************)
function THT1025.mSetFreq(freq: Double): Boolean;
begin
  result := DDSSetFrequency(mHnd, freq, mWaveFrmPoints, mNumPer);
end;

(******************************************************************************
HTSingleCyc: Set output for single wave, or continuous waveform output.
*******************************************************************************)
function THT1025.mSingleCyc(val: Boolean): Boolean;
begin
  result := DDSSetSingleWave(mHnd, val);
end;

(******************************************************************************
HTLoadPat: Loads a pattern to the HT1025 hardware. Returns true if successful.
*******************************************************************************)
function THT1025.mLoadPat(buf: ptrWord): Boolean;
begin
  result := DDSDownLoad(mHnd, buf, mWaveFrmPoints);
end;


(******************************************************************************
HTStatus: Check status of HT1025 specified.  Returns True if device responds.
*******************************************************************************)
function THT1025.mStatus(ID: Integer): Boolean;
begin
  result := DDSCheck(ID);
end;

(******************************************************************************
HTOutputOnStart: Tell the HT1025 if waveform should be output on start up.
*******************************************************************************)
function THT1025.mOutputOnStart(val: Boolean):Boolean;
begin
  result := DDSSetPowerOnOutput(mHnd, val);
end;

(******************************************************************************
HTSetTrigger: Tell the HT1025 to use internal or external trigger source, select
              edge (rising or falling)
*******************************************************************************)
function THT1025.mSetTrigger(ext: Boolean; edge:Boolean): Boolean;
begin
  result := DDSSetTrigger(mHnd, ext, edge);
end;





//DLL Function Implementations
 Function  DDSSearch; external 'HTDDSDll.dll';
 Function  DDSSetFrequency; external 'HTDDSDll.dll';
 Function  DDSGetMeasure; external 'HTDDSDll.dll';
 Function  DDSSetSingleWave; external 'HTDDSDll.dll';
 Function  DDSResetCounter; external 'HTDDSDll.dll';
 Function  DDSSetTrigger; external 'HTDDSDll.dll';
 Function  DDSGetDigitalIn; external 'HTDDSDll.dll';
 Function  DDSSetDigitalOut; external 'HTDDSDll.dll';
 Function  DDSDownload; external 'HTDDSDll.dll';
 Function  DDSSetDIOMode; external 'HTDDSDll.dll';
 Function  DDSCheck; external 'HTDDSDll.dll';
 Function  DDSSetPowerOnOutput; external 'HTDDSDll.dll';

end.
